import axios from '@/utils/api'

// 领养
// 添加宠物领养
const addPet = (formData) => {
    return axios.postForm("http://localhost:3000/api/addpet",  formData )
}
// 查看所有的领养信息
const getPets = (page, pageSize) => {
    return axios.get('http://localhost:3000/api/pets', { params: { page: page } })
}

// 添加宠物领养
const delPet = (id) => {
    return axios.post("http://localhost:3000/api/delpet", { id })
}

// 修改
const updatePet = (formData) => {
    return axios.postForm("http://localhost:3000/api/updatePet",  formData )
}

// 筛选
const selectPet = (name = undefined, variety = undefined, age = undefined, personality = undefined, health = undefined, address = undefined, page = 1) => {
    const query = { name, variety, age, personality, health, address };
    const filterQuery = Object.fromEntries(
        Object.entries(query).filter(([key, value]) => value)
    );
    return axios.post("http://localhost:3000/api/selectpet", { filterQuery, page })
}


// 送养
const addPetLost = (formData) => {
    return axios.postForm("http://localhost:3000/api/addpetlost",  formData )
}

// 查看所有的送养信息
const getPetsLost = (page, pageSize) => {
    return axios.get('http://localhost:3000/api/petslost', { params: { page: page } })
}

// 删除宠物送养
const delPetLost = (id) => {
    return axios.post("http://localhost:3000/api/delpetlost", { id })
}

// 修改
const updatePetLost = (formData) => {
    return axios.postForm("http://localhost:3000/api/updatePetlost",  formData )
}

// 筛选
const selectPetLost = (name = undefined, variety = undefined, age = undefined, personality = undefined, health = undefined, address = undefined, page = 1) => {
    const query = { name, variety, age, personality, health, address };
    const filterQuery = Object.fromEntries(
        Object.entries(query).filter(([key, value]) => value)
    );
    return axios.post("http://localhost:3000/api/selectpetlost", { filterQuery, page })
}

// 审批
const getExamine = (page) =>{
    return axios.get("http://localhost:3000/api/examine",{ params: { page: page } })
}
const updateExamine = (formData) => {
    return axios.post("http://localhost:3000/api/updateexamine",  formData )
}
const delExamine = (id) => {
    return axios.post("http://localhost:3000/api/delexamine", { id })
}
const selectExamine = (userName = undefined, userID = undefined, petName = undefined, status = undefined, page = 1) => {
    const query = { userName, userID, petName, status };
    const filterQuery = Object.fromEntries(
        Object.entries(query).filter(([key, value]) => value)
    );
    return axios.post("http://localhost:3000/api/selectexamine", { filterQuery, page })
}

// 聊天室
// 聊天列表
const chatsList = () =>{
    return axios.get("http://localhost:3000/api/allchat")
}
// 聊天室信息
const loadMessages = (roomId) =>{
    return axios.get("http://localhost:3000/api/chats",{ params: { roomId } })
}
// 更新消息列表
const updateChats = (roomId, chatsList)=>{
    return axios.post("http://localhost:3000/api/updatechats",{roomId, chatsList})
}


export {
    getPets, addPet, delPet, updatePet, selectPet,
    getPetsLost, addPetLost, delPetLost, updatePetLost, selectPetLost,
    getExamine,updateExamine,delExamine,selectExamine,
    chatsList,loadMessages,updateChats
}